package logic

import (
	"context"
	"group-card/common/constant"
	"group-card/common/request"
	"group-card/model/entity"
	"group-card/rpc/sys/internal/svc"
	"group-card/rpc/sys/sysclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CustomerListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCustomerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CustomerListLogic {
	return &CustomerListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CustomerListLogic) CustomerList(in *sysclient.CustomerListRequest) (*sysclient.CustomerListResponse, error) {
	var ctx context.Context
	req := &entity.SysCustomerReq{
		Id:     in.Id,
		Name:   in.Name,
		Area:   in.Area,
		Status: in.Status,
		BaseReq: request.BaseReq{
			PageNum:  in.PageNum,
			PageSize: in.PageSize,
		},
	}
	data, count, err := l.svcCtx.CustomerModel.List(ctx, req)
	if err != nil {
		return nil, err
	}

	var list []*sysclient.CustomerListData
	for _, value := range data {
		list = append(list, &sysclient.CustomerListData{
			Id:            value.Id,
			Name:          value.Name,
			MobilePhone:   value.MobilePhone,
			LandlinePhone: value.LandlinePhone,
			Area:          value.Area,
			Address:       value.Address,
			Email:         value.Email,
			BeginTimes:    value.BeginTimes.Format(constant.YMDHIS),
			EndTimes:      value.EndTimes.Format(constant.YMDHIS),
			Status:        value.Status,
			CreateAt:      value.CreateAt.Format(constant.YMDHIS),
			UpdateAt:      value.UpdateAt.Format(constant.YMDHIS),
			DeleteAt:      value.DeleteAt.Format(constant.YMDHIS),
		})
	}

	return &sysclient.CustomerListResponse{
		Total: count,
		List:  list,
	}, nil
}
